package com.bcfn.bshop.item.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.bcfn.bshop.common.constants.Constants;
import com.bcfn.bshop.common.dto.DataGridDto;
import com.bcfn.bshop.common.dto.MessageDto;
import com.bcfn.bshop.item.dto.ItemDto;
import com.bcfn.bshop.item.service.ItemService;

/**
 * 商品.
 * @author LiangGuoNing
 * @create 2019年2月8日 上午11:29:53
 * @copyright
 * @modify
 */
@RestController
public class ItemController {

	@Resource
	private ItemService bshopItemService;
	
	@PostMapping("/search")
	@ResponseBody
	public MessageDto<DataGridDto<ItemDto>> search(@RequestBody ItemDto itemDto){
		MessageDto<DataGridDto<ItemDto>> message = new MessageDto<DataGridDto<ItemDto>>();
		try {
			DataGridDto<ItemDto> dataGridDto = bshopItemService.search(itemDto);
			message.setCode(Constants.SUCCESS);
			message.setMessage("成功");
			message.setDatas(dataGridDto);
		}catch(Exception e) {
			e.printStackTrace();
			message.setCode(Constants.FAILURE);
			message.setMessage("失败");
		}
		return message;
	}
	
	/**
	 * 
	 * @param itemDto
	 * @return
	 */
	@PostMapping("/save")
	@ResponseBody
	public MessageDto<ItemDto> save(@RequestBody ItemDto itemDto) {
		MessageDto<ItemDto> message = new MessageDto<ItemDto>();
		try {
			bshopItemService.save(itemDto);
			message.setCode(Constants.SUCCESS);
			message.setMessage("保存成功!");
			message.setDatas(itemDto);
		}catch(Exception e) {
			message.setCode(Constants.FAILURE);
			message.setMessage("保存失败!");
		}
		return message;
	}
	
	@GetMapping("/get")
	@ResponseBody
	public MessageDto<ItemDto> get(Long id) {
		MessageDto<ItemDto> message = new MessageDto<ItemDto>();
		ItemDto itemDto = null;
		try {
			itemDto = bshopItemService.get(id);
			message.setCode(Constants.SUCCESS);
			message.setMessage("获取成功!");
			message.setDatas(itemDto);
		}catch(Exception e) {
			message.setCode(Constants.FAILURE);
			message.setMessage("获取失败!");
		}
		return message;
	}
	
	@GetMapping("/delete")
	@ResponseBody
	public MessageDto<String> delete(Long id){
		MessageDto<String> message = new MessageDto<String>();
		try {
			bshopItemService.delete(id);
			message.setCode(Constants.SUCCESS);
			message.setMessage("删除成功!");
			message.setDatas(message.getMessage());
		}catch(Exception e) {
			message.setCode(Constants.FAILURE);
			message.setMessage("删除失败!");
		}
		return message;
	}
}
